package com.actions.bluetoothbox2.fragment;

import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.actionbarsherlock.app.SherlockFragment;
import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.bluetoothbox2.util.ColorPickGradient;
import com.actions.bluetoothbox2.widget.ColorPickView;
import com.actions.ibluz.manager.BluzManager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by liuyong on 2017/6/15.
 */

public class LEDFragment extends SherlockFragment {

    private final static String TAG = "LEDFragment";
    private BrowserActivity mActivity;
    private View mView, view_color;
    private ColorPickView mColorPickView;
    private SeekBar seekbar_color;
    private RadioGroup rb_led_mode, rb_lights;
    private BluzManager mBluzManager;
    private ColorPickGradient mColorPickGradient;
    private RadioButton btn_music_led, btn_fast_led, btn_slow_led, btn_night_led;
    //btn_random_led, btn_breath_led,
    private int brightness = 255;
    private int red = 255;
    private int green = 255;
    private int blue = 255;
    private int mode;//0彩灯 1 关灯 2照明
    private int seekbar_pre = 0;
    private int select_color = 0xFFFFFF;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        mActivity = (BrowserActivity) getActivity();
        mView = inflater.inflate(R.layout.fragment_led, container, false);
        mActivity.mGlobalInfoLayout.setVisibility(View.GONE);
        mBluzManager = mActivity.getBluzManager();
        mActivity.tv_title.setText(getResources().getText(R.string.led));
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.title_back.setVisibility(View.VISIBLE);
        initView();
        return mView;
    }

    private void initView() {
        view_color = mView.findViewById(R.id.view_color);
        mColorPickView = (ColorPickView) mView.findViewById(R.id.color_picker_view);
        btn_music_led = (RadioButton) mView.findViewById(R.id.btn_music_led);
//        btn_random_led = (RadioButton) mView.findViewById(R.id.btn_random_led);
//        btn_breath_led = (RadioButton) mView.findViewById(R.id.btn_breath_led);
        btn_fast_led = (RadioButton) mView.findViewById(R.id.btn_fast_led);
        btn_slow_led = (RadioButton) mView.findViewById(R.id.btn_slow_led);
        btn_night_led = (RadioButton) mView.findViewById(R.id.btn_night_led);

        mColorPickView.setOnColorChangedListener(new ColorPickView.OnColorChangedListener() {
            @Override
            public void onColorChange(int color) {
                int cmd_type = Integer.decode("0x53");
                int cmd_id = Integer.decode("0x91");
                select_color = color;

                if (mode == 0) {
                    setSeekbarBg(new int[]{color, 0xFF000000});
                    float radio = (float) seekbar_pre / seekbar_color.getMax();
                    int mColor = mColorPickGradient.getColor(radio);
                    view_color.setBackgroundColor(mColor);

                    red = (mColor & 0xff0000) >> 16;
                    green = (mColor & 0x00ff00) >> 8;
                    blue = (mColor & 0x0000ff);

                    int brightness = 0;
//                    Log.i("TAG", "red===" + red + "-green==" + green + "-blue==" + blue);
                    mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, brightness << 24 | red << 16 | green << 8 | blue, 0, null);
                }
            }
        });

        seekbar_color = (SeekBar) mView.findViewById(R.id.seekbar_color);
        rb_led_mode = (RadioGroup) mView.findViewById(R.id.rb_led_mode);
        rb_led_mode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int cmd_type = Integer.decode("0x53");
                int cmd_id = Integer.decode("0x90");
                switch (i) {
                    case R.id.btn_music_led:
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, null);
                        break;
//                    case R.id.btn_random_led:
//                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 1, 0, null);
//                        break;
//                    case R.id.btn_breath_led:
//                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 2, 0, null);
//                        break;
                    case R.id.btn_fast_led:
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 3, 0, null);
                        break;
                    case R.id.btn_slow_led:
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 4, 0, null);
                        break;
                    case R.id.btn_night_led:
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 5, 0, null);
                        break;
                    default:
                        break;
                }
            }
        });

        rb_lights = (RadioGroup) mView.findViewById(R.id.rb_lights);
        rb_lights.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int cmd_type = Integer.decode("0x53");
                int cmd_id = Integer.decode("0x91");
                switch (i) {
                    case R.id.btn_coloured_lights:
                        mode = 0;
                        int brightness_n = 0;
                        setSeekbarBg(new int[]{select_color, 0xFF000000});
                        view_color.setBackgroundColor(select_color);

                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, brightness_n << 24 | red << 16 | green << 8 | blue, 0, null);
                        break;
                    case R.id.btn_close_lights:
                        mode = 1;
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, null);
                        btn_music_led.setChecked(false);
//                        btn_random_led.setChecked(false);
//                        btn_breath_led.setChecked(false);
                        btn_fast_led.setChecked(false);
                        btn_slow_led.setChecked(false);
                        btn_night_led.setChecked(false);
                        break;
                    case R.id.btn_lighting_lights:
                        mode = 2;
                        int red = 0;
                        int green = 0;
                        int blue = 0;
                        setSeekbarBg(new int[]{0xFFFFFFFF, 0xFF000000});
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, brightness << 24 | red << 16 | green << 8 | blue, 0, null);
                        btn_music_led.setChecked(false);
//                        btn_random_led.setChecked(false);
//                        btn_breath_led.setChecked(false);
                        btn_fast_led.setChecked(false);
                        btn_slow_led.setChecked(false);
                        btn_night_led.setChecked(false);
                        break;
                    default:
                        break;
                }
            }
        });

        seekbar_color.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                seekbar_pre = i;
                if (mode == 0) {
                    float radio = (float) seekbar_pre / seekBar.getMax();
                    if(null == mColorPickGradient){
                        setSeekbarBg(new int[]{select_color, 0xFF000000});
                    }
                    int mColor = mColorPickGradient.getColor(radio);
                    view_color.setBackgroundColor(mColor);
                    int cmd_type = Integer.decode("0x53");
                    int cmd_id = Integer.decode("0x91");

                    red = (mColor & 0xff0000) >> 16;
                    green = (mColor & 0x00ff00) >> 8;
                    blue = (mColor & 0x0000ff);
                    int brightness = 0;
//                    Log.i("TAG", "red===" + red + "-green==" + green + "-blue==" + blue);
                    mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, brightness << 24 | red << 16 | green << 8 | blue, 0, null);

                } else if (mode == 2) {
                    brightness = 255 - i;
                    int cmd_type = Integer.decode("0x53");
                    int cmd_id = Integer.decode("0x91");
                    int red = 0;
                    int green = 0;
                    int blue = 0;
                    mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, brightness << 24 | red << 16 | green << 8 | blue, 0, null);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    private void setSeekbarBg(final int[] mColorArr) {
        mColorPickGradient = new ColorPickGradient(mColorArr);
        ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
            @Override
            public Shader resize(int width, int height) {
                LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                        mColorArr, ColorPickGradient.PICKCOLORBAR_POSITIONS, Shader.TileMode.REPEAT);
                return linearGradient;
            }
        };
        PaintDrawable paint = new PaintDrawable();
        RectShape mRectShape = new RectShape();
        paint.setShape(mRectShape);
        paint.setCornerRadius(5);
        paint.setShaderFactory(shaderFactory);
        seekbar_color.setProgressDrawable(paint);
    }

    public void onDestroy() {
        mActivity.mGlobalInfoLayout.setVisibility(View.VISIBLE);
        super.onDestroy();
    }


}
